package com.emagic.manage.common.ui.base;

import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.StringRes;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.emagic.manage.R;


/**
 * 带ToolBar的Activity基类
 *
 * @author baoy
 * @version 1.0
 *          Create by 15/12/14 下午4:39
 */
public class ToolBarActivity extends AppStateActivity {

    public Toolbar mToolbar;

    private TextView mToolbarTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolBar();
    }

    private void initToolBar() {
        /*通过inflater获取toolbar的布局文件*/
        View toolbar = getLayoutInflater().inflate(R.layout.appbar_layout, getRootView());
        mToolbar = (Toolbar) toolbar.findViewById(R.id.id_tool_bar);
        mToolbarTitle = (TextView) toolbar.findViewById(R.id.tool_bar_title);

        mToolbar.setTitle("");
        //设置toolbar到Activity
        setSupportActionBar(mToolbar);
    }


    public Toolbar getToolbar() {
        return mToolbar;
    }

    public void setToolbarTitle(String title) {
        this.mToolbarTitle.setText(title);
    }

    public void setToolbarTitle(@StringRes int res) {
        this.mToolbarTitle.setText(res);
    }

    public void setToolbarTitleColor(@ColorInt int color) {
        mToolbarTitle.setTextColor(color);
    }

    public void setToolbarTitleColorRes(@ColorRes int id) {
        int color = getResources().getColor(id);
        mToolbarTitle.setTextColor(color);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
